package com.jams.goodox.activity.mine.personal;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.ProgressDialogUtils;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.SyllabearEditText;
import com.jams.goodox.widget.SyllabearTextType;
import com.jams.goodox.widget.VisageBeastTextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//修改昵称
public class EditPersonalInfoActivity extends BaseActivity {

    private static final String TAG = EditPersonalInfoActivity.class.getSimpleName();
    @Bind(R.id.inputBox)
    SyllabearEditText inputBox;
    @Bind(R.id.save)
    VisageBeastTextView save;

    boolean isTeacher;
    boolean isTouxian;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_personal_info;
    }

    @Override
    protected void init() {

        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_theme));//沉侵式头部
        //String nickName = getIntent().getStringExtra("nickname");
        isTouxian = getIntent().getBooleanExtra("isTouxian",false);
        inputBox.initEditText(SePref.getNickName());

        isTeacher = SePref.getMemberType() == 2 ? true :false;

        if(isTouxian){
            StyleUtil.titleBackKey(this, "头衔介绍");
        }else {
            StyleUtil.titleBackKey(this, getString(R.string.edit_nick_name));
        }
    }

    @OnClick(R.id.save)
    public void onViewClicked() {
        if (TextUtils.isEmpty(inputBox.getText())) {
            ToastUtil.showShort(mContext, "请输入昵称");
            return;
        }

        updatainfo(inputBox.getText());

    }


    //修改年级请求
    private void updatainfo(final String inf0) {


        Map<String,RequestBody> params = new HashMap<>();
        params.put("sign", RequestBody.create(MediaType.parse("multipart/form-data"), "test"));
        params.put("uuid", RequestBody.create(MediaType.parse("multipart/form-data"), "5617222222"));
        params.put("version", RequestBody.create(MediaType.parse("multipart/form-data"), "1.0"));
        params.put("accessToken", RequestBody.create(MediaType.parse("multipart/form-data"), SePref.getToken()));

        StringBuilder pData = new StringBuilder();
        pData.append("id:" + SePref.getId());
        pData.append(",gender:" + 1);

        pData.append(",memberType:" + (isTeacher? 2:1));
        if(isTouxian){
            pData.append(",nickName:" + SePref.getNickName());
        }else {
            pData.append(",nickName:" + inf0);
        }

        if(isTeacher){
            if(isTouxian){
                pData.append(",individualitySign:" + inf0);
            }else {
                pData.append(",individualitySign:" + SePref.getIndividualitySign());
            }

        }else {
            pData.append(",grade:" + SePref.getGrade());
            pData.append(",city:" + SePref.getCity());
        }

        params.put("requestData", RequestBody.create(MediaType.parse("multipart/form-data"), "{" + pData.toString() + "}"));

//        File file = new File(headImagePath);
//        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

//        MultipartBody.Part filePart = MultipartBody.Part.createFormData("files", file.getName(), requestFile);

        showLoading();
        // 执行请求
        Call<ResponseBody> call = createCallService(LoginService.class).updataMemberInfo(params,null);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                misLoading();
                try {
                    String data = response.body().string();
                    Log.d(TAG, "updataMemberInfo data = " + data);

                    JSONObject jsonObject = new JSONObject(data);

                    if(200 == jsonObject.getInt("code") &&
                            "true".equals(jsonObject.getString("responseData"))){

                        if(isTouxian){
                            SePref.putIndividualitySign(inf0);
                        }else {
                            SePref.setNickName(inf0);
                        }
                        setResult(RESULT_OK);
                        finish();

                        //nickname.setRightText(object.getString("nickName"));

//                        if(jsonObject.has("certPic")){
//                            SePref.setHeadImageUrl(jsonObject.getString("certPic"));
//                            headimg.setImageURI(jsonObject.getString("certPic"));
//                        }

                    }else {
                        if(jsonObject.has("error")){
                            ToastUtil.show("error:" + jsonObject.getString("error") );
                        }else {
                            ToastUtil.show("网络异常！" );
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                Log.d("Upload error:", t.getMessage());
            }
        });

    }

}
